<template>
  <el-row class="parent" :gutter="30">
    <el-col :span="4">
      <CategoryTree />
    </el-col>

    <el-col :span="20">
      <div>
        <el-button icon="el-icon-plus" type="primary" size="small" @click="add">添加</el-button>
        <el-button icon="el-icon-delete" type="danger" size="small" @click="delAll">删除</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column align="left" label="ID" width="100px">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>

        <el-table-column label="名称" width="120px">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>

        <el-table-column label="分类描述" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.remark }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="created_at" label="创建时间" width="200px">
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span>{{ new Date(parseInt(scope.row.createTime)).toLocaleString() }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="180px">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="toEdit(scope.row.id)">编辑</el-button>
            <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="del(scope.row.id)">
              <el-button slot="reference" type="danger" size="small">删除</el-button>
            </el-popconfirm>

          </template>
        </el-table-column>

      </el-table>

      <div class="block">
        <el-pagination
          :current-page="page.current"
          :page-sizes="page.sizes"
          :page-size="page.size"
          :total="page.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-col>

  </el-row>
</template>

<script>

import { getPage, deleteById, deleteAll } from '@/api/blog/category'
import CategoryTree from './tree'
import Bus from '@/utils/bus'

export default {
  components: { CategoryTree },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      page: {
        total: 10000,
        current: 1,
        size: 10,
        sizes: [10, 20, 50, 100, 500]
      },
      params: {},
      list: null,
      listLoading: true
    }
  },
  created() {
    this.fetchData()
  },
  mounted() {
    Bus.$on('selectedCategory', (data) => {
      this.selected(data.id)
    })
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getPage(this.params, this.page.current, this.page.size).then(response => {
        this.list = response.data
        this.page.total = response.pageInfo.total
      }).finally(() => {
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    },
    add() {
      this.$router.push({ name: 'categoryEdit', params: { id: 0, parentId: this.params.parentId }})
    },
    toEdit(id) {
      this.$router.push({ name: 'categoryEdit', params: { id: id }})
    },
    del(id) {
      deleteById(id).then(() => {
        this.fetchData()
      })
    },
    delAll() {
      const modelList = this.$refs.userTable.selection
      const idList = []
      modelList.forEach((model) => idList.push(model.id))
      if (idList.length < 1) {
        return
      }
      this.$confirm('确定要删除' + idList.length + '条数据？').then(_ => {
        deleteAll(idList).then((res) => {
          if (res.code === 0) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      }).catch(_ => {})
    },
    selected(data) {
      this.params.parentId = data
      this.fetchData()
    }
  }
}
</script>
<style>
  .parent {
    padding: 20px;
  }
</style>
